package com.lianjia.sh.se.esb.server.kafka;

import kafka.serializer.StringEncoder;
import kafka.tools.ConsoleProducer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * Created by yangsp1 on 16/7/20.
 */
public class ProducerManager {
    public Producer<String, String> createProducer() {
        Properties props = new Properties();
        //props.put("bootstrap.servers", "10.8.1.93:19092");
        //kafka集群ip配置
        props.put("bootstrap.servers", "10.8.1.160:19092,10.8.1.93:19092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("batch.size", 16384);
        props.put("linger.ms", 1);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        return new KafkaProducer<>(props);
    }

    public static void main(String args[]){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        ProducerManager producerManager = new ProducerManager();
        Producer producer = producerManager.createProducer();
        producer.send(new ProducerRecord<String, String>("my-topic", "老杨叔叔", "用心感受大自然的力量,发送时间［" + time + "］"));
        producer.close();
    }
}
